{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "52924083-876e-4fe1-874a-4da53b819c39",
   "metadata": {},
   "outputs": [],
   "source": [
    "import kotlin.random.Random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f3f5cd41",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"nHU03n\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"library\">\n",
       "       if(!window.letsPlotCallQueue) {\n",
       "           window.letsPlotCallQueue = [];\n",
       "       }; \n",
       "       window.letsPlotCall = function(f) {\n",
       "           window.letsPlotCallQueue.push(f);\n",
       "       };\n",
       "       (function() {\n",
       "           var script = document.createElement(\"script\");\n",
       "           script.type = \"text/javascript\";\n",
       "           script.src = \"https://cdn.jsdelivr.net/gh/JetBrains/lets-plot@v4.4.0/js-package/distr/lets-plot.min.js\";\n",
       "           script.onload = function() {\n",
       "               window.letsPlotCall = function(f) {f();};\n",
       "               window.letsPlotCallQueue.forEach(function(f) {f();});\n",
       "               window.letsPlotCallQueue = [];\n",
       "               \n",
       "               \n",
       "           };\n",
       "           script.onerror = function(event) {\n",
       "               window.letsPlotCall = function(f) {};\n",
       "               window.letsPlotCallQueue = [];\n",
       "               var div = document.createElement(\"div\");\n",
       "               div.style.color = 'darkred';\n",
       "               div.textContent = 'Error loading Lets-Plot JS';\n",
       "               document.getElementById(\"nHU03n\").appendChild(div);\n",
       "           };\n",
       "           var e = document.getElementById(\"nHU03n\");\n",
       "           e.appendChild(script);\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "            <div id=\"kotlin_out_0\"></div>\n",
       "            <script type=\"text/javascript\">\n",
       "                            if(!window.kotlinQueues) {\n",
       "                window.kotlinQueues = {};\n",
       "            }\n",
       "            if(!window.kotlinQueues[\"DataFrame\"]) {\n",
       "                var resQueue = [];\n",
       "                window.kotlinQueues[\"DataFrame\"] = resQueue;\n",
       "                window[\"call_DataFrame\"] = function(f) {\n",
       "                    resQueue.push(f);\n",
       "                }\n",
       "            }\n",
       "            (function (){\n",
       "                var modifiers = [(function(script) {\n",
       "    script.src = \"https://cdn.jsdelivr.net/gh/Kotlin/dataframe@3db46ccccaa1291c0627307d64133317f545e6ae/core/src/main/resources/init.js\"\n",
       "    script.type = \"text/javascript\";\n",
       "})];\n",
       "                var e = document.getElementById(\"kotlin_out_0\");\n",
       "                modifiers.forEach(function (gen) {\n",
       "                    var script = document.createElement(\"script\");\n",
       "                    gen(script)\n",
       "                    script.addEventListener(\"load\", function() {\n",
       "                        window[\"call_DataFrame\"] = function(f) {f();};\n",
       "                        window.kotlinQueues[\"DataFrame\"].forEach(function(f) {f();});\n",
       "                        window.kotlinQueues[\"DataFrame\"] = [];\n",
       "                    }, false);\n",
       "                    script.addEventListener(\"error\", function() {\n",
       "                        window[\"call_DataFrame\"] = function(f) {};\n",
       "                        window.kotlinQueues[\"DataFrame\"] = [];\n",
       "                        var div = document.createElement(\"div\");\n",
       "                        div.style.color = 'darkred';\n",
       "                        div.textContent = 'Error loading resource DataFrame';\n",
       "                        document.getElementById(\"kotlin_out_0\").appendChild(div);\n",
       "                    }, false);\n",
       "                    \n",
       "                    e.appendChild(script);\n",
       "                });\n",
       "            })();\n",
       "            </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "                <style>\n",
       "                :root {\n",
       "    --background: #fff;\n",
       "    --background-odd: #f5f5f5;\n",
       "    --background-hover: #d9edfd;\n",
       "    --header-text-color: #474747;\n",
       "    --text-color: #848484;\n",
       "    --text-color-dark: #000;\n",
       "    --text-color-medium: #737373;\n",
       "    --text-color-pale: #b3b3b3;\n",
       "    --inner-border-color: #aaa;\n",
       "    --bold-border-color: #000;\n",
       "    --link-color: #296eaa;\n",
       "    --link-color-pale: #296eaa;\n",
       "    --link-hover: #1a466c;\n",
       "}\n",
       "\n",
       ":root[theme=\"dark\"], :root [data-jp-theme-light=\"false\"], .dataframe_dark{\n",
       "    --background: #303030;\n",
       "    --background-odd: #3c3c3c;\n",
       "    --background-hover: #464646;\n",
       "    --header-text-color: #dddddd;\n",
       "    --text-color: #b3b3b3;\n",
       "    --text-color-dark: #dddddd;\n",
       "    --text-color-medium: #b2b2b2;\n",
       "    --text-color-pale: #737373;\n",
       "    --inner-border-color: #707070;\n",
       "    --bold-border-color: #777777;\n",
       "    --link-color: #008dc0;\n",
       "    --link-color-pale: #97e1fb;\n",
       "    --link-hover: #00688e;\n",
       "}\n",
       "\n",
       "p.dataframe_description {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe {\n",
       "    font-family: \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n",
       "    font-size: 12px;\n",
       "    background-color: var(--background);\n",
       "    color: var(--text-color-dark);\n",
       "    border: none;\n",
       "    border-collapse: collapse;\n",
       "}\n",
       "\n",
       "table.dataframe th, td {\n",
       "    padding: 6px;\n",
       "    border: 1px solid transparent;\n",
       "    text-align: left;\n",
       "}\n",
       "\n",
       "table.dataframe th {\n",
       "    background-color: var(--background);\n",
       "    color: var(--header-text-color);\n",
       "}\n",
       "\n",
       "table.dataframe td {\n",
       "    vertical-align: top;\n",
       "}\n",
       "\n",
       "table.dataframe th.bottomBorder {\n",
       "    border-bottom-color: var(--bold-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:nth-child(odd) {\n",
       "    background: var(--background-odd);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:nth-child(even) {\n",
       "    background: var(--background);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:hover {\n",
       "    background: var(--background-hover);\n",
       "}\n",
       "\n",
       "table.dataframe a {\n",
       "    cursor: pointer;\n",
       "    color: var(--link-color);\n",
       "    text-decoration: none;\n",
       "}\n",
       "\n",
       "table.dataframe tr:hover > td a {\n",
       "    color: var(--link-color-pale);\n",
       "}\n",
       "\n",
       "table.dataframe a:hover {\n",
       "    color: var(--link-hover);\n",
       "    text-decoration: underline;\n",
       "}\n",
       "\n",
       "table.dataframe img {\n",
       "    max-width: fit-content;\n",
       "}\n",
       "\n",
       "table.dataframe th.complex {\n",
       "    background-color: var(--background);\n",
       "    border: 1px solid var(--background);\n",
       "}\n",
       "\n",
       "table.dataframe .leftBorder {\n",
       "    border-left-color: var(--inner-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe .rightBorder {\n",
       "    border-right-color: var(--inner-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe .rightAlign {\n",
       "    text-align: right;\n",
       "}\n",
       "\n",
       "table.dataframe .expanderSvg {\n",
       "    width: 8px;\n",
       "    height: 8px;\n",
       "    margin-right: 3px;\n",
       "}\n",
       "\n",
       "table.dataframe .expander {\n",
       "    display: flex;\n",
       "    align-items: center;\n",
       "}\n",
       "\n",
       "/* formatting */\n",
       "\n",
       "table.dataframe .null {\n",
       "    color: var(--text-color-pale);\n",
       "}\n",
       "\n",
       "table.dataframe .structural {\n",
       "    color: var(--text-color-medium);\n",
       "    font-weight: bold;\n",
       "}\n",
       "\n",
       "table.dataframe .dataFrameCaption {\n",
       "    font-weight: bold;\n",
       "}\n",
       "\n",
       "table.dataframe .numbers {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe td:hover .formatted .structural, .null {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe tr:hover .formatted .structural, .null {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "\n",
       "                </style>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%useLatestDescriptors\n",
    "%use lets-plot\n",
    "%use dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "06df3cb5-087d-4741-9670-c65e49ec9bee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Lets-Plot Kotlin API v.0.0.0-SNAPSHOT. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.4.0."
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LetsPlot.getInfo()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15508d12-1922-4c06-b9c4-6643387922a8",
   "metadata": {},
   "source": [
    "# Overriding aesthetics in guideLegend()¶\n",
    "You can set specific aesthetic values for legend keys."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3031c6a-3ef7-4ca2-bd5c-912c9ac4dfe3",
   "metadata": {},
   "source": [
    "## Demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b0516089-88bf-4a33-acfb-fab6ecefee14",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/kotlindataframe+json": "{\"nrow\":3,\"ncol\":12,\"columns\":[\"untitled\",\"manufacturer\",\"model\",\"displ\",\"year\",\"cyl\",\"trans\",\"drv\",\"cty\",\"hwy\",\"fl\",\"class\"],\"kotlin_dataframe\":[{\"untitled\":1,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"auto(l5)\",\"drv\":\"f\",\"cty\":18,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":2,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"manual(m5)\",\"drv\":\"f\",\"cty\":21,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":3,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.0,\"year\":2008,\"cyl\":4,\"trans\":\"manual(m6)\",\"drv\":\"f\",\"cty\":20,\"hwy\":31,\"fl\":\"p\",\"class\":\"compact\"}]}",
      "text/html": [
       "        <html>\n",
       "        <head>\n",
       "            <style type=\"text/css\">\n",
       "                :root {\n",
       "    --background: #fff;\n",
       "    --background-odd: #f5f5f5;\n",
       "    --background-hover: #d9edfd;\n",
       "    --header-text-color: #474747;\n",
       "    --text-color: #848484;\n",
       "    --text-color-dark: #000;\n",
       "    --text-color-medium: #737373;\n",
       "    --text-color-pale: #b3b3b3;\n",
       "    --inner-border-color: #aaa;\n",
       "    --bold-border-color: #000;\n",
       "    --link-color: #296eaa;\n",
       "    --link-color-pale: #296eaa;\n",
       "    --link-hover: #1a466c;\n",
       "}\n",
       "\n",
       ":root[theme=\"dark\"], :root [data-jp-theme-light=\"false\"], .dataframe_dark{\n",
       "    --background: #303030;\n",
       "    --background-odd: #3c3c3c;\n",
       "    --background-hover: #464646;\n",
       "    --header-text-color: #dddddd;\n",
       "    --text-color: #b3b3b3;\n",
       "    --text-color-dark: #dddddd;\n",
       "    --text-color-medium: #b2b2b2;\n",
       "    --text-color-pale: #737373;\n",
       "    --inner-border-color: #707070;\n",
       "    --bold-border-color: #777777;\n",
       "    --link-color: #008dc0;\n",
       "    --link-color-pale: #97e1fb;\n",
       "    --link-hover: #00688e;\n",
       "}\n",
       "\n",
       "p.dataframe_description {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe {\n",
       "    font-family: \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n",
       "    font-size: 12px;\n",
       "    background-color: var(--background);\n",
       "    color: var(--text-color-dark);\n",
       "    border: none;\n",
       "    border-collapse: collapse;\n",
       "}\n",
       "\n",
       "table.dataframe th, td {\n",
       "    padding: 6px;\n",
       "    border: 1px solid transparent;\n",
       "    text-align: left;\n",
       "}\n",
       "\n",
       "table.dataframe th {\n",
       "    background-color: var(--background);\n",
       "    color: var(--header-text-color);\n",
       "}\n",
       "\n",
       "table.dataframe td {\n",
       "    vertical-align: top;\n",
       "}\n",
       "\n",
       "table.dataframe th.bottomBorder {\n",
       "    border-bottom-color: var(--bold-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:nth-child(odd) {\n",
       "    background: var(--background-odd);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:nth-child(even) {\n",
       "    background: var(--background);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:hover {\n",
       "    background: var(--background-hover);\n",
       "}\n",
       "\n",
       "table.dataframe a {\n",
       "    cursor: pointer;\n",
       "    color: var(--link-color);\n",
       "    text-decoration: none;\n",
       "}\n",
       "\n",
       "table.dataframe tr:hover > td a {\n",
       "    color: var(--link-color-pale);\n",
       "}\n",
       "\n",
       "table.dataframe a:hover {\n",
       "    color: var(--link-hover);\n",
       "    text-decoration: underline;\n",
       "}\n",
       "\n",
       "table.dataframe img {\n",
       "    max-width: fit-content;\n",
       "}\n",
       "\n",
       "table.dataframe th.complex {\n",
       "    background-color: var(--background);\n",
       "    border: 1px solid var(--background);\n",
       "}\n",
       "\n",
       "table.dataframe .leftBorder {\n",
       "    border-left-color: var(--inner-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe .rightBorder {\n",
       "    border-right-color: var(--inner-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe .rightAlign {\n",
       "    text-align: right;\n",
       "}\n",
       "\n",
       "table.dataframe .expanderSvg {\n",
       "    width: 8px;\n",
       "    height: 8px;\n",
       "    margin-right: 3px;\n",
       "}\n",
       "\n",
       "table.dataframe .expander {\n",
       "    display: flex;\n",
       "    align-items: center;\n",
       "}\n",
       "\n",
       "/* formatting */\n",
       "\n",
       "table.dataframe .null {\n",
       "    color: var(--text-color-pale);\n",
       "}\n",
       "\n",
       "table.dataframe .structural {\n",
       "    color: var(--text-color-medium);\n",
       "    font-weight: bold;\n",
       "}\n",
       "\n",
       "table.dataframe .dataFrameCaption {\n",
       "    font-weight: bold;\n",
       "}\n",
       "\n",
       "table.dataframe .numbers {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe td:hover .formatted .structural, .null {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe tr:hover .formatted .structural, .null {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "\n",
       ":root {\n",
       "    --background: #fff;\n",
       "    --background-odd: #f5f5f5;\n",
       "    --background-hover: #d9edfd;\n",
       "    --header-text-color: #474747;\n",
       "    --text-color: #848484;\n",
       "    --text-color-dark: #000;\n",
       "    --text-color-medium: #737373;\n",
       "    --text-color-pale: #b3b3b3;\n",
       "    --inner-border-color: #aaa;\n",
       "    --bold-border-color: #000;\n",
       "    --link-color: #296eaa;\n",
       "    --link-color-pale: #296eaa;\n",
       "    --link-hover: #1a466c;\n",
       "}\n",
       "\n",
       ":root[theme=\"dark\"], :root [data-jp-theme-light=\"false\"], .dataframe_dark{\n",
       "    --background: #303030;\n",
       "    --background-odd: #3c3c3c;\n",
       "    --background-hover: #464646;\n",
       "    --header-text-color: #dddddd;\n",
       "    --text-color: #b3b3b3;\n",
       "    --text-color-dark: #dddddd;\n",
       "    --text-color-medium: #b2b2b2;\n",
       "    --text-color-pale: #737373;\n",
       "    --inner-border-color: #707070;\n",
       "    --bold-border-color: #777777;\n",
       "    --link-color: #008dc0;\n",
       "    --link-color-pale: #97e1fb;\n",
       "    --link-hover: #00688e;\n",
       "}\n",
       "\n",
       "p.dataframe_description {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe {\n",
       "    font-family: \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n",
       "    font-size: 12px;\n",
       "    background-color: var(--background);\n",
       "    color: var(--text-color-dark);\n",
       "    border: none;\n",
       "    border-collapse: collapse;\n",
       "}\n",
       "\n",
       "table.dataframe th, td {\n",
       "    padding: 6px;\n",
       "    border: 1px solid transparent;\n",
       "    text-align: left;\n",
       "}\n",
       "\n",
       "table.dataframe th {\n",
       "    background-color: var(--background);\n",
       "    color: var(--header-text-color);\n",
       "}\n",
       "\n",
       "table.dataframe td {\n",
       "    vertical-align: top;\n",
       "}\n",
       "\n",
       "table.dataframe th.bottomBorder {\n",
       "    border-bottom-color: var(--bold-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:nth-child(odd) {\n",
       "    background: var(--background-odd);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:nth-child(even) {\n",
       "    background: var(--background);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:hover {\n",
       "    background: var(--background-hover);\n",
       "}\n",
       "\n",
       "table.dataframe a {\n",
       "    cursor: pointer;\n",
       "    color: var(--link-color);\n",
       "    text-decoration: none;\n",
       "}\n",
       "\n",
       "table.dataframe tr:hover > td a {\n",
       "    color: var(--link-color-pale);\n",
       "}\n",
       "\n",
       "table.dataframe a:hover {\n",
       "    color: var(--link-hover);\n",
       "    text-decoration: underline;\n",
       "}\n",
       "\n",
       "table.dataframe img {\n",
       "    max-width: fit-content;\n",
       "}\n",
       "\n",
       "table.dataframe th.complex {\n",
       "    background-color: var(--background);\n",
       "    border: 1px solid var(--background);\n",
       "}\n",
       "\n",
       "table.dataframe .leftBorder {\n",
       "    border-left-color: var(--inner-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe .rightBorder {\n",
       "    border-right-color: var(--inner-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe .rightAlign {\n",
       "    text-align: right;\n",
       "}\n",
       "\n",
       "table.dataframe .expanderSvg {\n",
       "    width: 8px;\n",
       "    height: 8px;\n",
       "    margin-right: 3px;\n",
       "}\n",
       "\n",
       "table.dataframe .expander {\n",
       "    display: flex;\n",
       "    align-items: center;\n",
       "}\n",
       "\n",
       "/* formatting */\n",
       "\n",
       "table.dataframe .null {\n",
       "    color: var(--text-color-pale);\n",
       "}\n",
       "\n",
       "table.dataframe .structural {\n",
       "    color: var(--text-color-medium);\n",
       "    font-weight: bold;\n",
       "}\n",
       "\n",
       "table.dataframe .dataFrameCaption {\n",
       "    font-weight: bold;\n",
       "}\n",
       "\n",
       "table.dataframe .numbers {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe td:hover .formatted .structural, .null {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe tr:hover .formatted .structural, .null {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "\n",
       "            </style>\n",
       "        </head>\n",
       "        <body>\n",
       "            <table class=\"dataframe\" id=\"df_-335544320\"></table>\n",
       "\n",
       "<p class=\"dataframe_description\">DataFrame: rowsCount = 3, columnsCount = 12</p>\n",
       "<table class=\"dataframe\" id=\"static_df_-335544319\"><thead><tr><th class=\"bottomBorder\" style=\"text-align:left\">untitled</th><th class=\"bottomBorder\" style=\"text-align:left\">manufacturer</th><th class=\"bottomBorder\" style=\"text-align:left\">model</th><th class=\"bottomBorder\" style=\"text-align:left\">displ</th><th class=\"bottomBorder\" style=\"text-align:left\">year</th><th class=\"bottomBorder\" style=\"text-align:left\">cyl</th><th class=\"bottomBorder\" style=\"text-align:left\">trans</th><th class=\"bottomBorder\" style=\"text-align:left\">drv</th><th class=\"bottomBorder\" style=\"text-align:left\">cty</th><th class=\"bottomBorder\" style=\"text-align:left\">hwy</th><th class=\"bottomBorder\" style=\"text-align:left\">fl</th><th class=\"bottomBorder\" style=\"text-align:left\">class</th></tr></thead><tbody><tr><td  style=\"vertical-align:top\">1</td><td  style=\"vertical-align:top\">audi</td><td  style=\"vertical-align:top\">a4</td><td  style=\"vertical-align:top\">1,800000</td><td  style=\"vertical-align:top\">1999</td><td  style=\"vertical-align:top\">4</td><td  style=\"vertical-align:top\">auto(l5)</td><td  style=\"vertical-align:top\">f</td><td  style=\"vertical-align:top\">18</td><td  style=\"vertical-align:top\">29</td><td  style=\"vertical-align:top\">p</td><td  style=\"vertical-align:top\">compact</td></tr><tr><td  style=\"vertical-align:top\">2</td><td  style=\"vertical-align:top\">audi</td><td  style=\"vertical-align:top\">a4</td><td  style=\"vertical-align:top\">1,800000</td><td  style=\"vertical-align:top\">1999</td><td  style=\"vertical-align:top\">4</td><td  style=\"vertical-align:top\">manual(m5)</td><td  style=\"vertical-align:top\">f</td><td  style=\"vertical-align:top\">21</td><td  style=\"vertical-align:top\">29</td><td  style=\"vertical-align:top\">p</td><td  style=\"vertical-align:top\">compact</td></tr><tr><td  style=\"vertical-align:top\">3</td><td  style=\"vertical-align:top\">audi</td><td  style=\"vertical-align:top\">a4</td><td  style=\"vertical-align:top\">2,000000</td><td  style=\"vertical-align:top\">2008</td><td  style=\"vertical-align:top\">4</td><td  style=\"vertical-align:top\">manual(m6)</td><td  style=\"vertical-align:top\">f</td><td  style=\"vertical-align:top\">20</td><td  style=\"vertical-align:top\">31</td><td  style=\"vertical-align:top\">p</td><td  style=\"vertical-align:top\">compact</td></tr></tbody></table>\n",
       "        </body>\n",
       "        <script>\n",
       "            /*<!--*/\n",
       "call_DataFrame(function() { DataFrame.addTable({ cols: [{ name: \"<span title=\\\"untitled: Int\\\">untitled</span>\", children: [], rightAlign: true, values: [\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">1</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">2</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">3</span></span>\"] }, \n",
       "{ name: \"<span title=\\\"manufacturer: String\\\">manufacturer</span>\", children: [], rightAlign: false, values: [\"audi\",\"audi\",\"audi\"] }, \n",
       "{ name: \"<span title=\\\"model: String\\\">model</span>\", children: [], rightAlign: false, values: [\"a4\",\"a4\",\"a4\"] }, \n",
       "{ name: \"<span title=\\\"displ: Double\\\">displ</span>\", children: [], rightAlign: true, values: [\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">1,8</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">1,8</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">2,0</span></span>\"] }, \n",
       "{ name: \"<span title=\\\"year: Int\\\">year</span>\", children: [], rightAlign: true, values: [\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">1999</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">1999</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">2008</span></span>\"] }, \n",
       "{ name: \"<span title=\\\"cyl: Int\\\">cyl</span>\", children: [], rightAlign: true, values: [\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">4</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">4</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">4</span></span>\"] }, \n",
       "{ name: \"<span title=\\\"trans: String\\\">trans</span>\", children: [], rightAlign: false, values: [\"auto(l5)\",\"manual(m5)\",\"manual(m6)\"] }, \n",
       "{ name: \"<span title=\\\"drv: String\\\">drv</span>\", children: [], rightAlign: false, values: [\"f\",\"f\",\"f\"] }, \n",
       "{ name: \"<span title=\\\"cty: Int\\\">cty</span>\", children: [], rightAlign: true, values: [\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">18</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">21</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">20</span></span>\"] }, \n",
       "{ name: \"<span title=\\\"hwy: Int\\\">hwy</span>\", children: [], rightAlign: true, values: [\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">29</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">29</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">31</span></span>\"] }, \n",
       "{ name: \"<span title=\\\"fl: String\\\">fl</span>\", children: [], rightAlign: false, values: [\"p\",\"p\",\"p\"] }, \n",
       "{ name: \"<span title=\\\"class: String\\\">class</span>\", children: [], rightAlign: false, values: [\"compact\",\"compact\",\"compact\"] }, \n",
       "], id: -335544320, rootId: -335544320, totalRows: 3 } ) });\n",
       "/*-->*/\n",
       "\n",
       "call_DataFrame(function() { DataFrame.renderTable(-335544320) });\n",
       "\n",
       "document.getElementById(\"static_df_-335544319\").style.display = \"none\";\n",
       "        </script>\n",
       "        </html>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val mpgDf = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n",
    "val mpgData = mpgDf.toMap()\n",
    "mpgDf.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "24bcdf82-a6a8-4327-97a5-470d32b2e741",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"9P8aTj\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"displ\",\n",
       "\"y\":\"hwy\",\n",
       "\"color\":\"drv\"\n",
       "},\n",
       "\"data\":{\n",
       "\"displ\":[1.8,1.8,2.0,2.0,2.8,2.8,3.1,1.8,1.8,2.0,2.0,2.8,2.8,3.1,3.1,2.8,3.1,4.2,5.3,5.3,5.3,5.7,6.0,5.7,5.7,6.2,6.2,7.0,5.3,5.3,5.7,6.5,2.4,2.4,3.1,3.5,3.6,2.4,3.0,3.3,3.3,3.3,3.3,3.3,3.8,3.8,3.8,4.0,3.7,3.7,3.9,3.9,4.7,4.7,4.7,5.2,5.2,3.9,4.7,4.7,4.7,5.2,5.7,5.9,4.7,4.7,4.7,4.7,4.7,4.7,5.2,5.2,5.7,5.9,4.6,5.4,5.4,4.0,4.0,4.0,4.0,4.6,5.0,4.2,4.2,4.6,4.6,4.6,5.4,5.4,3.8,3.8,4.0,4.0,4.6,4.6,4.6,4.6,5.4,1.6,1.6,1.6,1.6,1.6,1.8,1.8,1.8,2.0,2.4,2.4,2.4,2.4,2.5,2.5,3.3,2.0,2.0,2.0,2.0,2.7,2.7,2.7,3.0,3.7,4.0,4.7,4.7,4.7,5.7,6.1,4.0,4.2,4.4,4.6,5.4,5.4,5.4,4.0,4.0,4.6,5.0,2.4,2.4,2.5,2.5,3.5,3.5,3.0,3.0,3.5,3.3,3.3,4.0,5.6,3.1,3.8,3.8,3.8,5.3,2.5,2.5,2.5,2.5,2.5,2.5,2.2,2.2,2.5,2.5,2.5,2.5,2.5,2.5,2.7,2.7,3.4,3.4,4.0,4.7,2.2,2.2,2.4,2.4,3.0,3.0,3.5,2.2,2.2,2.4,2.4,3.0,3.0,3.3,1.8,1.8,1.8,1.8,1.8,4.7,5.7,2.7,2.7,2.7,3.4,3.4,4.0,4.0,2.0,2.0,2.0,2.0,2.8,1.9,2.0,2.0,2.0,2.0,2.5,2.5,2.8,2.8,1.9,1.9,2.0,2.0,2.5,2.5,1.8,1.8,2.0,2.0,2.8,2.8,3.6],\n",
       "\"drv\":[\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"r\",\"r\",\"r\",\"r\",\"r\",\"r\",\"r\",\"r\",\"r\",\"r\",\"4\",\"4\",\"4\",\"4\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"r\",\"r\",\"r\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"r\",\"r\",\"r\",\"r\",\"r\",\"r\",\"r\",\"r\",\"r\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"r\",\"r\",\"r\",\"4\",\"4\",\"4\",\"4\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"4\",\"4\",\"4\",\"4\",\"f\",\"f\",\"f\",\"f\",\"f\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\"],\n",
       "\"hwy\":[29.0,29.0,31.0,30.0,26.0,26.0,27.0,26.0,25.0,28.0,27.0,25.0,25.0,25.0,25.0,24.0,25.0,23.0,20.0,15.0,20.0,17.0,17.0,26.0,23.0,26.0,25.0,24.0,19.0,14.0,15.0,17.0,27.0,30.0,26.0,29.0,26.0,24.0,24.0,22.0,22.0,24.0,24.0,17.0,22.0,21.0,23.0,23.0,19.0,18.0,17.0,17.0,19.0,19.0,12.0,17.0,15.0,17.0,17.0,12.0,17.0,16.0,18.0,15.0,16.0,12.0,17.0,17.0,16.0,12.0,15.0,16.0,17.0,15.0,17.0,17.0,18.0,17.0,19.0,17.0,19.0,19.0,17.0,17.0,17.0,16.0,16.0,17.0,15.0,17.0,26.0,25.0,26.0,24.0,21.0,22.0,23.0,22.0,20.0,33.0,32.0,32.0,29.0,32.0,34.0,36.0,36.0,29.0,26.0,27.0,30.0,31.0,26.0,26.0,28.0,26.0,29.0,28.0,27.0,24.0,24.0,24.0,22.0,19.0,20.0,17.0,12.0,19.0,18.0,14.0,15.0,18.0,18.0,15.0,17.0,16.0,18.0,17.0,19.0,19.0,17.0,29.0,27.0,31.0,32.0,27.0,26.0,26.0,25.0,25.0,17.0,17.0,20.0,18.0,26.0,26.0,27.0,28.0,25.0,25.0,24.0,27.0,25.0,26.0,23.0,26.0,26.0,26.0,26.0,25.0,27.0,25.0,27.0,20.0,20.0,19.0,17.0,20.0,17.0,29.0,27.0,31.0,31.0,26.0,26.0,28.0,27.0,29.0,31.0,31.0,26.0,26.0,27.0,30.0,33.0,35.0,37.0,35.0,15.0,18.0,20.0,20.0,22.0,17.0,19.0,18.0,20.0,29.0,26.0,29.0,29.0,24.0,44.0,29.0,26.0,29.0,29.0,29.0,29.0,23.0,24.0,44.0,41.0,29.0,26.0,28.0,29.0,29.0,29.0,28.0,29.0,26.0,26.0,26.0]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"size\":4.0,\n",
       "\"alpha\":0.2,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"stroke\":0.0,\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"untitled\"\n",
       "},{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"manufacturer\"\n",
       "},{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"model\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"displ\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"year\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"cyl\"\n",
       "},{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"trans\"\n",
       "},{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"drv\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"cty\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"hwy\"\n",
       "},{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"fl\"\n",
       "},{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"class\"\n",
       "}]\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"9P8aTj\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// By default alpha is not applied to the legend.\n",
    "\n",
    "val baseMpgPlot = letsPlot(mpgData) { x = \"displ\"; y = \"hwy\"; color = \"drv\" } +\n",
    "    geomPoint(size = 4, alpha = 0.2, stroke = 0)\n",
    "\n",
    "baseMpgPlot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e8dd95ce-d2f5-4663-8e76-0d51f0a203dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"l8O3ud\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"displ\",\n",
       "\"y\":\"hwy\",\n",
       "\"color\":\"drv\"\n",
       "},\n",
       "\"guides\":{\n",
       "\"color\":{\n",
       "\"name\":\"legend\",\n",
       "\"override_aes\":{\n",
       "\"size\":8.0,\n",
       "\"alpha\":0.4\n",
       "}\n",
       "}\n",
       "},\n",
       "\"data\":{\n",
       "\"displ\":[1.8,1.8,2.0,2.0,2.8,2.8,3.1,1.8,1.8,2.0,2.0,2.8,2.8,3.1,3.1,2.8,3.1,4.2,5.3,5.3,5.3,5.7,6.0,5.7,5.7,6.2,6.2,7.0,5.3,5.3,5.7,6.5,2.4,2.4,3.1,3.5,3.6,2.4,3.0,3.3,3.3,3.3,3.3,3.3,3.8,3.8,3.8,4.0,3.7,3.7,3.9,3.9,4.7,4.7,4.7,5.2,5.2,3.9,4.7,4.7,4.7,5.2,5.7,5.9,4.7,4.7,4.7,4.7,4.7,4.7,5.2,5.2,5.7,5.9,4.6,5.4,5.4,4.0,4.0,4.0,4.0,4.6,5.0,4.2,4.2,4.6,4.6,4.6,5.4,5.4,3.8,3.8,4.0,4.0,4.6,4.6,4.6,4.6,5.4,1.6,1.6,1.6,1.6,1.6,1.8,1.8,1.8,2.0,2.4,2.4,2.4,2.4,2.5,2.5,3.3,2.0,2.0,2.0,2.0,2.7,2.7,2.7,3.0,3.7,4.0,4.7,4.7,4.7,5.7,6.1,4.0,4.2,4.4,4.6,5.4,5.4,5.4,4.0,4.0,4.6,5.0,2.4,2.4,2.5,2.5,3.5,3.5,3.0,3.0,3.5,3.3,3.3,4.0,5.6,3.1,3.8,3.8,3.8,5.3,2.5,2.5,2.5,2.5,2.5,2.5,2.2,2.2,2.5,2.5,2.5,2.5,2.5,2.5,2.7,2.7,3.4,3.4,4.0,4.7,2.2,2.2,2.4,2.4,3.0,3.0,3.5,2.2,2.2,2.4,2.4,3.0,3.0,3.3,1.8,1.8,1.8,1.8,1.8,4.7,5.7,2.7,2.7,2.7,3.4,3.4,4.0,4.0,2.0,2.0,2.0,2.0,2.8,1.9,2.0,2.0,2.0,2.0,2.5,2.5,2.8,2.8,1.9,1.9,2.0,2.0,2.5,2.5,1.8,1.8,2.0,2.0,2.8,2.8,3.6],\n",
       "\"drv\":[\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"r\",\"r\",\"r\",\"r\",\"r\",\"r\",\"r\",\"r\",\"r\",\"r\",\"4\",\"4\",\"4\",\"4\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"r\",\"r\",\"r\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"r\",\"r\",\"r\",\"r\",\"r\",\"r\",\"r\",\"r\",\"r\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"r\",\"r\",\"r\",\"4\",\"4\",\"4\",\"4\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"4\",\"4\",\"4\",\"4\",\"f\",\"f\",\"f\",\"f\",\"f\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\",\"f\"],\n",
       "\"hwy\":[29.0,29.0,31.0,30.0,26.0,26.0,27.0,26.0,25.0,28.0,27.0,25.0,25.0,25.0,25.0,24.0,25.0,23.0,20.0,15.0,20.0,17.0,17.0,26.0,23.0,26.0,25.0,24.0,19.0,14.0,15.0,17.0,27.0,30.0,26.0,29.0,26.0,24.0,24.0,22.0,22.0,24.0,24.0,17.0,22.0,21.0,23.0,23.0,19.0,18.0,17.0,17.0,19.0,19.0,12.0,17.0,15.0,17.0,17.0,12.0,17.0,16.0,18.0,15.0,16.0,12.0,17.0,17.0,16.0,12.0,15.0,16.0,17.0,15.0,17.0,17.0,18.0,17.0,19.0,17.0,19.0,19.0,17.0,17.0,17.0,16.0,16.0,17.0,15.0,17.0,26.0,25.0,26.0,24.0,21.0,22.0,23.0,22.0,20.0,33.0,32.0,32.0,29.0,32.0,34.0,36.0,36.0,29.0,26.0,27.0,30.0,31.0,26.0,26.0,28.0,26.0,29.0,28.0,27.0,24.0,24.0,24.0,22.0,19.0,20.0,17.0,12.0,19.0,18.0,14.0,15.0,18.0,18.0,15.0,17.0,16.0,18.0,17.0,19.0,19.0,17.0,29.0,27.0,31.0,32.0,27.0,26.0,26.0,25.0,25.0,17.0,17.0,20.0,18.0,26.0,26.0,27.0,28.0,25.0,25.0,24.0,27.0,25.0,26.0,23.0,26.0,26.0,26.0,26.0,25.0,27.0,25.0,27.0,20.0,20.0,19.0,17.0,20.0,17.0,29.0,27.0,31.0,31.0,26.0,26.0,28.0,27.0,29.0,31.0,31.0,26.0,26.0,27.0,30.0,33.0,35.0,37.0,35.0,15.0,18.0,20.0,20.0,22.0,17.0,19.0,18.0,20.0,29.0,26.0,29.0,29.0,24.0,44.0,29.0,26.0,29.0,29.0,29.0,29.0,23.0,24.0,44.0,41.0,29.0,26.0,28.0,29.0,29.0,29.0,28.0,29.0,26.0,26.0,26.0]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"size\":4.0,\n",
       "\"alpha\":0.2,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"stroke\":0.0,\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"untitled\"\n",
       "},{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"manufacturer\"\n",
       "},{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"model\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"displ\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"year\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"cyl\"\n",
       "},{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"trans\"\n",
       "},{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"drv\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"cty\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"hwy\"\n",
       "},{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"fl\"\n",
       "},{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"class\"\n",
       "}]\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"l8O3ud\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Applying alpha and size for better look.\n",
    "\n",
    "baseMpgPlot + guides(color = guideLegend(alpha = 0.4, size = 8.0))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5b38a9a-5835-4030-85ce-0fcc9c8602c9",
   "metadata": {},
   "source": [
    "## Experiments with parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f8b4c449",
   "metadata": {},
   "outputs": [],
   "source": [
    "Random(1)\n",
    "val n = 4\n",
    "val x = List(n) { it }\n",
    "val y = List(n) { Random.nextDouble() }\n",
    "val v = List(n) { Random.nextDouble() }\n",
    "val data = mapOf(\"x\" to x, \"y\" to y, \"v\" to v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4e04a12d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"dsvYyE\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0],\n",
       "\"y\":[0.001921890939868276,0.0868525910333181,0.650297104154425,0.3983989995255278]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":400.0,\n",
       "\"height\":200.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"color\":\"x\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"mapping_annotations\":[{\n",
       "\"aes\":\"color\",\n",
       "\"annotation\":\"as_discrete\",\n",
       "\"parameters\":{\n",
       "\"label\":\"x\"\n",
       "}\n",
       "}]\n",
       "},\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"v\"\n",
       "}]\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"dsvYyE\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Base plot\n",
    "val p = letsPlot(data) { x = \"x\"; y = \"y\" } +\n",
    "    geomPoint { color = asDiscrete(\"x\") } +\n",
    "    ggsize(400, 200)\n",
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "04c6a892",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"euy0tS\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"guides\":{\n",
       "\"color\":{\n",
       "\"name\":\"legend\",\n",
       "\"override_aes\":{\n",
       "\"shape\":22.0,\n",
       "\"size\":8.0,\n",
       "\"alpha\":0.6,\n",
       "\"fill\":\"light-grey\",\n",
       "\"stroke\":1.8\n",
       "}\n",
       "}\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0],\n",
       "\"y\":[0.001921890939868276,0.0868525910333181,0.650297104154425,0.3983989995255278]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":400.0,\n",
       "\"height\":200.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"color\":\"x\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"mapping_annotations\":[{\n",
       "\"aes\":\"color\",\n",
       "\"annotation\":\"as_discrete\",\n",
       "\"parameters\":{\n",
       "\"label\":\"x\"\n",
       "}\n",
       "}]\n",
       "},\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"v\"\n",
       "}]\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"euy0tS\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Specifying common aesthetics for all the legend labels.\n",
    "\n",
    "p + guides(color = guideLegend(shape = 22, size = 8.0, stroke = 1.8, fill = \"light-grey\", alpha = 0.6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "98097225",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"D6a2oe\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"guides\":{\n",
       "\"color\":{\n",
       "\"name\":\"legend\",\n",
       "\"override_aes\":{\n",
       "\"color\":[\"red\",\"blue\",\"green\",\"pink\"]\n",
       "}\n",
       "}\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0],\n",
       "\"y\":[0.001921890939868276,0.0868525910333181,0.650297104154425,0.3983989995255278]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":400.0,\n",
       "\"height\":200.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"color\":\"x\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"mapping_annotations\":[{\n",
       "\"aes\":\"color\",\n",
       "\"annotation\":\"as_discrete\",\n",
       "\"parameters\":{\n",
       "\"label\":\"x\"\n",
       "}\n",
       "}]\n",
       "},\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"v\"\n",
       "}]\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"D6a2oe\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Using a list of values.\n",
    "\n",
    "p + guides(color = guideLegend(color = listOf(\"red\", \"blue\", \"green\", \"pink\")))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "89ef8a97-02cc-4d23-9b84-fccb493628a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"SmNteS\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"guides\":{\n",
       "\"color\":{\n",
       "\"name\":\"legend\",\n",
       "\"override_aes\":{\n",
       "\"color\":[\"red\",null,\"green\",null]\n",
       "}\n",
       "}\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0],\n",
       "\"y\":[0.001921890939868276,0.0868525910333181,0.650297104154425,0.3983989995255278]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":400.0,\n",
       "\"height\":200.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"color\":\"x\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"mapping_annotations\":[{\n",
       "\"aes\":\"color\",\n",
       "\"annotation\":\"as_discrete\",\n",
       "\"parameters\":{\n",
       "\"label\":\"x\"\n",
       "}\n",
       "}]\n",
       "},\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"v\"\n",
       "}]\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"SmNteS\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Use `null` to keep the original value.\n",
    "\n",
    "p + guides(color = guideLegend(color = listOf(\"red\", null, \"green\", null)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6e9cb997",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"pag008\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"guides\":{\n",
       "\"color\":{\n",
       "\"name\":\"legend\",\n",
       "\"override_aes\":{\n",
       "\"color\":[\"grey\"],\n",
       "\"size\":[10.0,8.0]\n",
       "}\n",
       "}\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0],\n",
       "\"y\":[0.001921890939868276,0.0868525910333181,0.650297104154425,0.3983989995255278]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":400.0,\n",
       "\"height\":200.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"color\":\"x\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"mapping_annotations\":[{\n",
       "\"aes\":\"color\",\n",
       "\"annotation\":\"as_discrete\",\n",
       "\"parameters\":{\n",
       "\"label\":\"x\"\n",
       "}\n",
       "}]\n",
       "},\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"v\"\n",
       "}]\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"pag008\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Using a list that is smaller than the number of legend keys.\n",
    "// The last value spreads to the rest of the keys.\n",
    "\n",
    "p + guides(color = guideLegend(color = listOf(\"grey\"), size = listOf(10.0, 8.0)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "fa2b1a19-995c-4596-8dfc-d6831b46b646",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"6VLeCq\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"guides\":{\n",
       "\"color\":{\n",
       "\"name\":\"legend\",\n",
       "\"override_aes\":{\n",
       "\"color\":[\"grey\",null],\n",
       "\"size\":[10.0,null,8.0]\n",
       "}\n",
       "}\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0],\n",
       "\"y\":[0.001921890939868276,0.0868525910333181,0.650297104154425,0.3983989995255278]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":400.0,\n",
       "\"height\":200.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"color\":\"x\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"mapping_annotations\":[{\n",
       "\"aes\":\"color\",\n",
       "\"annotation\":\"as_discrete\",\n",
       "\"parameters\":{\n",
       "\"label\":\"x\"\n",
       "}\n",
       "}]\n",
       "},\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"v\"\n",
       "}]\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"6VLeCq\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Using a list that is smaller than the number of legend keys.\n",
    "// `null` can be used to stop value propagation; the rest keys will get their original values.\n",
    "\n",
    "p + guides(color = guideLegend(color = listOf(\"grey\", null), size = listOf(10.0, null, 8.0)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccebef12-d878-48aa-a497-627d284d2fbc",
   "metadata": {},
   "source": [
    "## Reproduced an example from [rbind.io](https://aosmith.rbind.io/2020/07/09/ggplot2-override-aes/)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "612092f4-190b-411c-bda3-6f5e57d8ef2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"zRMq0Y\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\",\n",
       "\"color\":\"g1\",\n",
       "\"shape\":\"g2\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[0.42,0.39,0.56,0.59,0.17,0.95,0.85,0.25,0.31,0.75,0.58,0.9,0.6,0.86,0.61,0.61],\n",
       "\"y\":[-1.4,3.6,1.1,-0.1,0.5,0.0,-1.8,0.8,-1.1,-0.6,0.2,0.3,1.1,1.6,0.9,-0.6],\n",
       "\"g1\":[\"High\",\"Low\",\"High\",\"Low\",\"High\",\"Low\",\"High\",\"Low\",\"High\",\"Low\",\"High\",\"Low\",\"High\",\"Low\",\"High\",\"Low\"],\n",
       "\"g2\":[\"Control\",\"Control\",\"Treatment\",\"Treatment\",\"Control\",\"Control\",\"Treatment\",\"Treatment\",\"Control\",\"Control\",\"Treatment\",\"Treatment\",\"Control\",\"Control\",\"Treatment\",\"Treatment\"]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"size\":5.0,\n",
       "\"alpha\":0.6,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"g1\"\n",
       "},{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"g2\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"zRMq0Y\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val dat = mapOf(\n",
    "        \"g1\" to listOf(\"High\", \"Low\", \"High\", \"Low\", \"High\", \"Low\", \"High\", \"Low\", \"High\", \"Low\", \"High\", \"Low\", \"High\", \"Low\", \"High\", \"Low\"),\n",
    "        \"g2\" to listOf(\"Control\", \"Control\", \"Treatment\", \"Treatment\", \"Control\", \"Control\", \"Treatment\", \"Treatment\", \"Control\", \"Control\", \"Treatment\", \"Treatment\", \"Control\", \"Control\", \"Treatment\", \"Treatment\"),\n",
    "        \"x\" to listOf(0.42, 0.39, 0.56, 0.59, 0.17, 0.95, 0.85, 0.25, 0.31, 0.75, 0.58, 0.9, 0.6, 0.86, 0.61, 0.61),\n",
    "        \"y\" to listOf(-1.4, 3.6, 1.1, -0.1, 0.5, 0.0, -1.8, 0.8, -1.1, -0.6, 0.2, 0.3, 1.1, 1.6, 0.9, -0.6)\n",
    "    )\n",
    "\n",
    "val bp = letsPlot(dat) { x = \"x\"; y = \"y\"; color = \"g1\"; shape = \"g2\" } +\n",
    "        geomPoint(size = 5.0, alpha = 0.6)\n",
    "bp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "16324c8c-df68-4159-b4d0-9eb50790bf8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"vR2oN7\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\",\n",
       "\"fill\":\"g1\",\n",
       "\"shape\":\"g2\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[0.42,0.39,0.56,0.59,0.17,0.95,0.85,0.25,0.31,0.75,0.58,0.9,0.6,0.86,0.61,0.61],\n",
       "\"y\":[-1.4,3.6,1.1,-0.1,0.5,0.0,-1.8,0.8,-1.1,-0.6,0.2,0.3,1.1,1.6,0.9,-0.6],\n",
       "\"g1\":[\"High\",\"Low\",\"High\",\"Low\",\"High\",\"Low\",\"High\",\"Low\",\"High\",\"Low\",\"High\",\"Low\",\"High\",\"Low\",\"High\",\"Low\"],\n",
       "\"g2\":[\"Control\",\"Control\",\"Treatment\",\"Treatment\",\"Control\",\"Control\",\"Treatment\",\"Treatment\",\"Control\",\"Control\",\"Treatment\",\"Treatment\",\"Control\",\"Control\",\"Treatment\",\"Treatment\"]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"fill\",\n",
       "\"values\":[\"#002F70\",\"#EDB4B5\"]\n",
       "},{\n",
       "\"aesthetic\":\"shape\",\n",
       "\"values\":[21.0,24.0]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"color\":\"black\",\n",
       "\"size\":5.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"g1\"\n",
       "},{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"g2\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"vR2oN7\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Customized scales and default legend do not correspond well.\n",
    "\n",
    "val cp = letsPlot(dat) { x = \"x\"; y = \"y\"; fill = \"g1\"; shape = \"g2\" } +\n",
    "    geomPoint(size = 5.0, color = \"black\") +\n",
    "    scaleFillManual(values = listOf(\"#002F70\", \"#EDB4B5\")) +\n",
    "    scaleShapeManual(values = listOf(21, 24))\n",
    "\n",
    "cp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f4f54d77-5bd3-4477-83c2-8998da6fdab2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"l1VEE1\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\",\n",
       "\"fill\":\"g1\",\n",
       "\"shape\":\"g2\"\n",
       "},\n",
       "\"guides\":{\n",
       "\"shape\":{\n",
       "\"name\":\"legend\",\n",
       "\"override_aes\":{\n",
       "\"fill\":\"black\"\n",
       "}\n",
       "},\n",
       "\"fill\":{\n",
       "\"name\":\"legend\",\n",
       "\"override_aes\":{\n",
       "\"shape\":21.0\n",
       "}\n",
       "}\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[0.42,0.39,0.56,0.59,0.17,0.95,0.85,0.25,0.31,0.75,0.58,0.9,0.6,0.86,0.61,0.61],\n",
       "\"y\":[-1.4,3.6,1.1,-0.1,0.5,0.0,-1.8,0.8,-1.1,-0.6,0.2,0.3,1.1,1.6,0.9,-0.6],\n",
       "\"g1\":[\"High\",\"Low\",\"High\",\"Low\",\"High\",\"Low\",\"High\",\"Low\",\"High\",\"Low\",\"High\",\"Low\",\"High\",\"Low\",\"High\",\"Low\"],\n",
       "\"g2\":[\"Control\",\"Control\",\"Treatment\",\"Treatment\",\"Control\",\"Control\",\"Treatment\",\"Treatment\",\"Control\",\"Control\",\"Treatment\",\"Treatment\",\"Control\",\"Control\",\"Treatment\",\"Treatment\"]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"fill\",\n",
       "\"values\":[\"#002F70\",\"#EDB4B5\"]\n",
       "},{\n",
       "\"aesthetic\":\"shape\",\n",
       "\"values\":[21.0,24.0]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"color\":\"black\",\n",
       "\"size\":5.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"g1\"\n",
       "},{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"g2\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"l1VEE1\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// This can be fixed by overriding aesthetics.\n",
    "\n",
    "cp + guides(\n",
    "    fill = guideLegend(shape = 21),\n",
    "    shape = guideLegend(fill = \"black\")\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd3315a9-2a01-43e8-bf3e-749283bd94c8",
   "metadata": {},
   "source": [
    "## Testing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f2889194",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"ymWacD\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":2,\n",
       "\"nrow\":1,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data\":{\n",
       "\"n\":[\"a\",\"b\",\"c\"]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"size\":\"n\",\n",
       "\"fill\":\"n\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"pie\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"n\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"guides\":{\n",
       "\"size\":{\n",
       "\"name\":\"legend\",\n",
       "\"override_aes\":{\n",
       "\"color\":\"black\",\n",
       "\"stroke\":4.0\n",
       "}\n",
       "},\n",
       "\"fill\":{\n",
       "\"name\":\"legend\",\n",
       "\"override_aes\":{\n",
       "\"color\":\"blue\"\n",
       "}\n",
       "}\n",
       "},\n",
       "\"data\":{\n",
       "\"n\":[\"a\",\"b\",\"c\"]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"size\":\"n\",\n",
       "\"fill\":\"n\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"pie\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"n\"\n",
       "}]\n",
       "}\n",
       "}],\n",
       "\"ggsize\":{\n",
       "\"width\":800.0,\n",
       "\"height\":300.0\n",
       "},\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"ymWacD\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// `fill` and `size` are mapped to the same variable.\n",
    "// The result is not defined if the same legend is set via different aesthetics.\n",
    "\n",
    "val piePlot = letsPlot(mapOf(\"n\" to listOf(\"a\", \"b\", \"c\"))) + \n",
    "    geomPie(stat = Stat.identity) {fill = \"n\"; size = \"n\"}\n",
    "\n",
    "gggrid(listOf(\n",
    "    piePlot,\n",
    "    piePlot + guides(\n",
    "        fill = guideLegend(color = \"blue\"),\n",
    "        size = guideLegend(stroke = 4.0, color = \"black\")\n",
    "    )\n",
    ")) + ggsize(800, 300)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "12cd9926-b947-407d-8106-294b02c2a062",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"tt7hqy\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"guides\":{\n",
       "\"color\":{\n",
       "\"name\":\"legend\",\n",
       "\"override_aes\":{\n",
       "\"color\":[\"red\",\"blue\",\"green\",\"pink\"],\n",
       "\"shape\":22.0,\n",
       "\"size\":8.0,\n",
       "\"alpha\":0.4,\n",
       "\"fill\":\"white\",\n",
       "\"stroke\":1.8\n",
       "}\n",
       "}\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0],\n",
       "\"y\":[0.001921890939868276,0.0868525910333181,0.650297104154425,0.3983989995255278]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":400.0,\n",
       "\"height\":200.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"color\":\"x\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"mapping_annotations\":[{\n",
       "\"aes\":\"color\",\n",
       "\"annotation\":\"as_discrete\",\n",
       "\"parameters\":{\n",
       "\"label\":\"x\"\n",
       "}\n",
       "}]\n",
       "},\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"v\"\n",
       "}]\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"tt7hqy\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Verifying different aesthetics\n",
    "\n",
    "p + guides(\n",
    "    color = guideLegend(\n",
    "        color = listOf(\"red\", \"blue\", \"green\", \"pink\"),\n",
    "        shape = 22,\n",
    "        size = 8.0,\n",
    "        stroke = 1.8,\n",
    "        fill = \"white\",\n",
    "        alpha = 0.4\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7c7db11a-ad6d-44a4-aa2f-e6143c5aab1a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"15eCUa\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"guides\":{\n",
       "\"color\":{\n",
       "\"name\":\"legend\",\n",
       "\"override_aes\":{\n",
       "\"shape\":-11.0,\n",
       "\"size\":0.0,\n",
       "\"stroke\":-2.0\n",
       "}\n",
       "}\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0],\n",
       "\"y\":[0.001921890939868276,0.0868525910333181,0.650297104154425,0.3983989995255278]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":400.0,\n",
       "\"height\":200.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"color\":\"x\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"mapping_annotations\":[{\n",
       "\"aes\":\"color\",\n",
       "\"annotation\":\"as_discrete\",\n",
       "\"parameters\":{\n",
       "\"label\":\"x\"\n",
       "}\n",
       "}]\n",
       "},\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"v\"\n",
       "}]\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"15eCUa\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Invalid or null values\n",
    "\n",
    "p + guides(color=guideLegend(color = null, shape = -11, size = 0, stroke = -2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "7b57d837-9a59-431b-83f7-5e041032e200",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"BDuPTw\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"guides\":{\n",
       "\"color\":{\n",
       "\"name\":\"legend\",\n",
       "\"override_aes\":{\n",
       "\"color\":[]\n",
       "}\n",
       "}\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0],\n",
       "\"y\":[0.001921890939868276,0.0868525910333181,0.650297104154425,0.3983989995255278]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":400.0,\n",
       "\"height\":200.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"color\":\"x\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"mapping_annotations\":[{\n",
       "\"aes\":\"color\",\n",
       "\"annotation\":\"as_discrete\",\n",
       "\"parameters\":{\n",
       "\"label\":\"x\"\n",
       "}\n",
       "}]\n",
       "},\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"v\"\n",
       "}]\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"BDuPTw\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// An empty list of values\n",
    "\n",
    "p + guides(color=guideLegend(color=listOf<Int>()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "7a5ca960-6362-4317-9eb5-f0d99ea776da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"kdpD2j\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"guides\":{\n",
       "\"color\":{\n",
       "\"name\":\"legend\",\n",
       "\"override_aes\":{\n",
       "\"color\":[\"pink\",\"gray\",\"green\",\"orange\"]\n",
       "}\n",
       "}\n",
       "},\n",
       "\"data\":{\n",
       "\"category\":[\"A\",\"B\",\"C\",\"D\"],\n",
       "\"value\":[10.0,15.0,20.0,25.0]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":400.0,\n",
       "\"height\":200.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"color\",\n",
       "\"values\":[\"red\",\"blue\",\"green\",\"black\"],\n",
       "\"labels\":[\"Label A\",\"Label B\",\"Label C\",\"Label C\"]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"category\",\n",
       "\"y\":\"value\",\n",
       "\"color\":\"category\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"category\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"value\"\n",
       "}]\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"kdpD2j\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// `override_aes` in the case when some legend labels are combined into one;\n",
    "// see issue https://github.com/JetBrains/lets-plot/issues/1129 for details.\n",
    "\n",
    "val df = mapOf(\n",
    "    \"category\" to listOf(\"A\", \"B\", \"C\", \"D\"),\n",
    "    \"value\" to listOf(10, 15, 20, 25)\n",
    ")\n",
    "\n",
    "letsPlot(df) + ggsize(400, 200) +\n",
    "    geomPoint { x = \"category\"; y = \"value\"; color = \"category\" } +\n",
    "    scaleColorManual(\n",
    "        values = listOf(\"red\", \"blue\", \"green\", \"black\"),\n",
    "        labels = listOf(\"Label A\", \"Label B\", \"Label C\", \"Label C\")\n",
    "    ) +\n",
    "    guides(color = guideLegend(color = listOf(\"pink\", \"gray\", \"green\", \"orange\")))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f4a7d72-7dd4-4ee8-965d-bff65fa2eb74",
   "metadata": {},
   "source": [
    "## Custom legend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a113117f-67db-48e6-9cc2-9e443c5252a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"yUenh6\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":3,\n",
       "\"nrow\":1,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[2.0,3.0,5.0,7.0,11.0]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"manual_key\":\"Point\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"manual_key\":\"Line\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"line\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"guides\":{\n",
       "\"manual\":{\n",
       "\"name\":\"legend\",\n",
       "\"override_aes\":{\n",
       "\"size\":6.0\n",
       "}\n",
       "}\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[2.0,3.0,5.0,7.0,11.0]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"manual_key\":\"Point\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"manual_key\":\"Line\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"line\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"guides\":{\n",
       "\"manual\":{\n",
       "\"name\":\"legend\",\n",
       "\"override_aes\":{\n",
       "\"color\":[\"red\",\"blue\"],\n",
       "\"size\":[6.0,2.0]\n",
       "}\n",
       "}\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[2.0,3.0,5.0,7.0,11.0]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"manual_key\":\"Point\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"manual_key\":\"Line\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"line\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "}],\n",
       "\"ggsize\":{\n",
       "\"width\":900.0,\n",
       "\"height\":200.0\n",
       "},\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"yUenh6\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val data = mapOf(\n",
    "    \"x\" to listOf(1, 2, 3, 4, 5),\n",
    "    \"y\" to listOf(2, 3, 5, 7, 11)\n",
    ")\n",
    "\n",
    "// Verifying how `override_aes` works for custom legends.\n",
    "// Note that the same effect can be achieved easier by using direct parameters.\n",
    "\n",
    "val mPlot = letsPlot(data) { x = \"x\"; y = \"y\" } +\n",
    "    geomPoint(manualKey = \"Point\") +\n",
    "    geomLine(manualKey = \"Line\")\n",
    "\n",
    "gggrid(\n",
    "    listOf(\n",
    "        mPlot,\n",
    "        mPlot + guides(manual = guideLegend(size = 6.0)),\n",
    "        mPlot + guides(manual = guideLegend(size = listOf(6.0, 2.0), color = listOf(\"red\", \"blue\")))\n",
    "    )\n",
    ") + ggsize(900, 200)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "f274f43f-231e-4d78-a382-6c6b91bfeb6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"1LfURF\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":2,\n",
       "\"nrow\":1,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[2.0,3.0,5.0,7.0,11.0]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"manual_key\":{\n",
       "\"index\":1,\n",
       "\"label\":\"Point\"\n",
       "},\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"manual_key\":{\n",
       "\"index\":0,\n",
       "\"label\":\"Line\"\n",
       "},\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"line\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"guides\":{\n",
       "\"manual\":{\n",
       "\"name\":\"legend\",\n",
       "\"override_aes\":{\n",
       "\"color\":[\"red\",\"blue\"],\n",
       "\"size\":[2.0]\n",
       "}\n",
       "}\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[2.0,3.0,5.0,7.0,11.0]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"manual_key\":{\n",
       "\"index\":1,\n",
       "\"label\":\"Point\"\n",
       "},\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"manual_key\":{\n",
       "\"index\":0,\n",
       "\"label\":\"Line\"\n",
       "},\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"line\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "}],\n",
       "\"ggsize\":{\n",
       "\"width\":600.0,\n",
       "\"height\":200.0\n",
       "},\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"1LfURF\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Changing the order in the legend via `index` in `layer_key`\n",
    "\n",
    "val mPlot2 = letsPlot(data) { x = \"x\"; y = \"y\" } +\n",
    "    geomPoint(manualKey = layerKey(\"Point\", index = 1)) +\n",
    "    geomLine(manualKey = layerKey(\"Line\", index = 0))\n",
    "\n",
    "gggrid(\n",
    "    listOf(\n",
    "        mPlot2,\n",
    "        mPlot2 + guides(manual = guideLegend(size = listOf(2.0), color = listOf(\"red\", \"blue\")))\n",
    "    )\n",
    ") + ggsize(600, 200)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ab8d56a-eedc-4e80-b710-b31a9e0eddc2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.9.23"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
